package com.campus.utils;

import java.time.*;
import java.time.temporal.ChronoUnit;

public class AllUtils {

//    // 计算当前到下一个零点的秒数
//    public static  long calculateSecondsUntilMidnight() {
//        LocalTime now = LocalTime.now();
//        LocalTime midnight = LocalTime.MIDNIGHT;
//
//        Duration duration = Duration.between(now, midnight);
//        System.out.println(duration+"　　＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝");
//        long seconds = -duration.toHours() * 60 * 60;
//        System.out.println("距离凌晨还有："+seconds+"秒");
//        return seconds;
//    }
    /**
     * 计算当前时间到当天凌晨（第二天00:00:00）的小时数
     * @return 剩余小时数（带小数）
     */
    public static double getHoursToMidnight() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime nextMidnight = now.toLocalDate().plusDays(1).atStartOfDay();
        long seconds = ChronoUnit.SECONDS.between(now, nextMidnight);
        return seconds / 3600.0;
    }

    /**
     * 计算今天到本周最后一天（周日23:59:59）的小时数
     * @return 剩余小时数（带小数）
     */
    public static double getHoursToEndOfWeek() {
        LocalDateTime now = LocalDateTime.now();
        LocalDate today = now.toLocalDate();

        // 计算本周最后一天（周日）
        DayOfWeek currentDayOfWeek = today.getDayOfWeek();
        int daysUntilSunday = DayOfWeek.SUNDAY.getValue() - currentDayOfWeek.getValue();
        if (daysUntilSunday < 0) {
            daysUntilSunday += 7; // 如果已经是周日，就计算到下一周周日
        }

        LocalDateTime endOfWeek = today.plusDays(daysUntilSunday)
                .atTime(23, 59, 59);

        long seconds = ChronoUnit.SECONDS.between(now, endOfWeek);
        return seconds / 3600.0;
    }

    /**
     * 计算今天到本月最后一天（最后一天23:59:59）的小时数
     * @return 剩余小时数（带小数）
     */
    public static double getHoursToEndOfMonth() {
        LocalDateTime now = LocalDateTime.now();
        LocalDate today = now.toLocalDate();

        // 获取本月最后一天
        YearMonth currentYearMonth = YearMonth.from(today);
        LocalDate lastDayOfMonth = currentYearMonth.atEndOfMonth();

        LocalDateTime endOfMonth = lastDayOfMonth.atTime(23, 59, 59);

        long seconds = ChronoUnit.SECONDS.between(now, endOfMonth);
        return seconds / 3600.0;
    }

    /**
     * 重载方法 - 返回整数小时（向上取整）
     */
    public static long getHoursToMidnightCeil() {
        return (long) Math.ceil(getHoursToMidnight());
    }

    public static long getHoursToEndOfWeekCeil() {
        return (long) Math.ceil(getHoursToEndOfWeek());
    }

    public static long getHoursToEndOfMonthCeil() {
        return (long) Math.ceil(getHoursToEndOfMonth());
    }
}
